<?php
namespace Home\TagLib;
use Think\Template\TagLib;
use Common\Library\Category;
use Common\Library\Page;
class Mmcms extends TagLib{
	protected $tags=array(
		//横幅
		'banner'=>array('attr'=>'name,item','close'=>1),
		//商品分类
		'cate'=>array('attr'=>'name,key,category_id','close'=>1),
		//语言列表
		'languages'=>array('attr'=>'name','close'=>1),
		//导航
		'navigation'=>array('attr'=>'','close'=>1),
		//获取指定id的文章内容
		'article'=>array('attr'=>'id,width,height','close'=>1)
		);
	/**
	 * Banner
	 */
	public function _banner($tag,$content){
		$item=$tag['item'];
		$name=$tag['name'];
		$parse	= 	'<?php ';
		$parse 	.=	'$banner=D("Banner")->getBanner("'.$name.'");';
		$parse  .=	' ?>';
		$parse  .=	'<volist name="banner[\'banner_image\']" id="' . $item . '">';
		$parse  .=	$content;
		$parse  .=	'</volist>';
		return $parse;
	}
	/**
	 * 商品分类
	 */
	public function _cate($tag,$content){
		$name=$tag['name'];
		$key=$tag['key']?$tag['key']:'autoindex';
		$category_id=isset($tag['category_id'])?$tag['category_id']:0;
		$key=$tag['key'];
		$parse		=	'<?php ';
		$parse		.=	'$categories=D(\'Category\')->getCategories();';
		$parse		.=	'$categories=Common\Library\Category::unlimitedForLayer($categories,"child",'.$category_id.',"category_id","parent_id");';
		$parse		.=	' ?>';
		$parse  .=	'<volist name="categories" key="'.$key.'" id="' . $name . '">';
		$parse  .=	$content;
		$parse  .=	'</volist>';

		return $parse;
	}

	/**
	 * 语言
	 */
	public function _languages($tags,$content){
		$name=$tags['name'];
		$parse		=	'<?php ';
		$parse		.=	'$languages=get_languages();';
		$parse 		.=	' ?>';
		$parse 		.=	'<volist name="languages" id="'.$name.'">';
		$parse 		.=	$content;
		$parse 		.=	'</volist>';
		return $parse;
	}
	/**
	 * 导航
	 */
	public function _navigation($tags,$content){
		$parse		=	'<?php ';
		$parse 		.=	'$navigation=D("Navigation")->getNavigations();';
		$parse 		.=	' ?>';
		$parse 		.=	'<volist name="navigation" id="nav">';
		$parse		.=	$content;
		$parse		.=	'</volist>';
		return $parse;
	}
	/**
	 * 获取指定id的文章的内容
	 */
	public function _article($tags,$content){
		$article_id = $tags['id'];
		$width=isset($tags['width'])?$tags['width']:0;
		$height=isset($tags['height'])?$tags['height']:0;
		$parse	=	'<?php ';
		$parse	.=	'$article=D("Article")->getArticle(' . $article_id . ');';
		$parse	.=	'if($article && isset($article["article_image"])){ ';
		$parse	.=	'$article["image"]=image_resize($article["image"],0,0);';
		$parse 	.=	'foreach($article["article_image"] as $k=> $article_image){ ';
		$parse	.=	'$article["article_image"][$k]["thumb"]=image_resize($article_image["image"],' . $width . ', ' . $height . ' );';
		$parse 	.=	' }';
		$parse 	.=	' }';
		$parse	.=	' ?>';
		$parse 	.=	$content;
		$parse 	.=	'<?php unset($article);unset($k);unset($article_image); ?>';
		return $parse;
	}
}